# ================= 导入组件 ================
from rest_framework.viewsets import ModelViewSet
# ================= 序列化的类 ================
from basicweb.serializer import BranchAreaSerializer, AreaPlaceSerializer, BranchSerializer, \
    TaskStatusSerializer, TaskTypeSerializer, PlaceTypeSerializer
# =========== 导入数据类 ============
from basicweb.models import BranchArea, AreaPlace, Branch, TaskStatus, TaskType, PlaceType
# =========== 导入Filters =============
from basicweb.filters import BranchAreaFilter, AreaPlaceFilter
# ================ 导入action =================
from rest_framework.decorators import action
# ================ 导入Response ================
from rest_framework.response import Response



# # 场地类型
class PlaceTypeViews(ModelViewSet):
    queryset = PlaceType.objects.all()
    serializer_class = PlaceTypeSerializer


# # 任务类行
class TaskTypeViews(ModelViewSet):
    queryset = TaskType.objects.all()
    serializer_class = TaskTypeSerializer


# # 任务状态
class TaskStatusViews(ModelViewSet):
    queryset = TaskStatus.objects.all()
    serializer_class = TaskStatusSerializer


# # 分部
class BranchViews(ModelViewSet):
    queryset = Branch.objects.all()
    serializer_class = BranchSerializer


# # 区域
class BranchAreaViews(ModelViewSet):
    queryset = BranchArea.objects.all()
    serializer_class = BranchAreaSerializer

    filter_class = BranchAreaFilter


# # 场地
class AreaPlaceViews(ModelViewSet):
    queryset = AreaPlace.objects.all()
    serializer_class = AreaPlaceSerializer

    filter_class = AreaPlaceFilter

    @action(methods=['get'], detail=False)
    def select(self, request,*args, **kwargs):
        # 获取所有的分支机构
        branchs = list(Branch.objects.all().values("id", 'name'))
        # 获取所有区域
        areas = list(BranchArea.objects.all().values('id', 'name', 'branch_id'))
        # 获取场地
        places = list(AreaPlace.objects.all().values('code', 'name', 'brancharea_id'))
        # 定义集合
        data = []
        # 遍历
        for branch in branchs:
            # 定义一个集合
            temp_dict = {}
            temp_dict['value'] = str(branch.get('id')) + branch.get('name')
            temp_dict['label'] = branch.get('name')
            temp_dict['children'] = []
            # 遍历
            for area in areas:
                area_temp = {}
                # 判断
                if area['branch_id'] == branch.get('id'):
                    area_temp['value'] = str(area.get('id')) + area.get('name')
                    area_temp['label'] = area.get('name')
                    area_temp['children'] = []
                    # 判断
                    for place in places:
                        if place['brancharea_id'] == area.get('id'):
                            area_temp['children'].append({
                                "value": place.get('code'),
                                'label': place.get('name')
                            })
                # 判断
                if area_temp:
                    temp_dict['children'].append(area_temp)
            data.append(temp_dict)

        return Response({'data': data})




